﻿using FlowCanvas;
using FlowCanvas.Nodes;
using UnityEngine;

namespace Yoozoo.Gameplay.Liberty.StreetEvent
{
    public class GameObjectPlayAnimation : CallableActionNodeBase
    {
        protected override void OnRegisterPorts(FlowNode node)
        {
            var o = node.AddFlowOutput("In");
            var p1 = node.AddValueInput<GameObject>("gameObject");
            var p2 = node.AddValueInput<string>("animName");
            
            node.AddFlowInput("Out", (Flow f) => { Invoke(p1.value, p2.value); o.Call(f); });
        }

        private void Invoke(GameObject gameObject, string animName)
        {
            if (gameObject != null && !string.IsNullOrEmpty(animName.Trim()))
            {
                var animator = gameObject.GetComponentInChildren<Animator>();
                if (animator)
                {
                    animator.Play(animName.Trim());
                }
            }
        }
    }
}